/* $Revision: $ $Date: $
 * 
 * Copyright (C) 2009 Robert Stephan
 * 
 * This file is part of *** CaCaO - Card Catalogs Online ***
 *
 * CaCaO is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CaCao is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package cacao.client.ui;

import cacao.client.CaCaO;
import cacao.client.CaCaOConstants;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.LayoutPanel;

public class CaCaOMainComposite extends Composite implements CaCaOComposite{
	private CaCaO application;
	private LeftMenuCmp cmpLeftMenu;
	private BottomMenuCmp cmpBottomMenu;
	private TopMenuCmp cmpTopMenu;
	private ImageCmp cmpImage;
	private EditCardCmp cmpEdit;
	private CatalogAndSearchMenuCmp cmpCatalogSearch;
	private TopLeftCmp cmpTopLeft;
	private DockPanel dpBottom;
	public HandlerRegistration resizeHandler;
	private LayoutPanel p;

	public CaCaOMainComposite(CaCaO app){
		application = app;
		cmpTopLeft = new TopLeftCmp(application);			
		cmpLeftMenu = new LeftMenuCmp(application);
		cmpBottomMenu = new BottomMenuCmp(application);
		cmpTopMenu = new TopMenuCmp(application);
		cmpImage = new ImageCmp(application);
		cmpEdit = new EditCardCmp(application);
		cmpCatalogSearch =  new CatalogAndSearchMenuCmp(application);
		cmpBottomMenu.setImageCmp(cmpImage);

		p = new LayoutPanel();
		p.add(cmpTopLeft);
		p.setWidgetLeftWidth(cmpTopLeft, 0, Unit.PX, 240, Unit.PX);
		p.setWidgetTopHeight(cmpTopLeft, 0, Unit.PX, 90, Unit.PX);

		Label lblTopEmpty = new Label();
		lblTopEmpty.setStylePrimaryName("lbl-topEmpty");
		lblTopEmpty.setText(" ");
		p.add(lblTopEmpty);
		p.setWidgetLeftWidth(lblTopEmpty, 0, Unit.PX, 240, Unit.PX);
		p.setWidgetTopHeight(lblTopEmpty, 90, Unit.PX, 40, Unit.PX);
		
		p.add(cmpLeftMenu);
		p.setWidgetLeftWidth(cmpLeftMenu, 0, Unit.PX, 240, Unit.PX);
		p.setWidgetTopBottom(cmpLeftMenu, 130, Unit.PX, 0, Unit.PX);

		p.add(cmpCatalogSearch);
		p.setWidgetLeftRight(cmpCatalogSearch, 240, Unit.PX, 0, Unit.PX);
		p.setWidgetTopHeight(cmpCatalogSearch, 0, Unit.PX, 90, Unit.PX);
		p.add(cmpTopMenu);
		p.setWidgetLeftRight(cmpTopMenu, 240, Unit.PX, 0, Unit.PX);
		p.setWidgetTopHeight(cmpTopMenu, 90, Unit.PX, 40, Unit.PX);
		p.add(cmpImage);		
		p.setWidgetLeftRight(cmpImage, 240, Unit.PX, 0, Unit.PX);
		p.setWidgetTopBottom(cmpImage, 130, Unit.PX, 90, Unit.PX);

		dpBottom = new DockPanel();
		dpBottom.setWidth("100%");
		dpBottom.setHeight("100%");
		dpBottom.add(cmpEdit, DockPanel.CENTER);
		dpBottom.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		dpBottom.add(cmpBottomMenu, DockPanel.EAST);

		p.add(dpBottom);		
		p.setWidgetLeftRight(dpBottom, 240, Unit.PX, 0, Unit.PX);
		p.setWidgetBottomHeight(dpBottom, 0, Unit.PX, 90, Unit.PX);

		initWidget(p);
		
	}

	public void updateUI(){
		AsyncCallback<String> callback = new AsyncCallback<String>(){
			public void onSuccess(String result){
				if(result !=null && application.getCurrentItem()!=null ){ 
					//edit mode
					p.setWidgetBottomHeight(dpBottom, 0, Unit.PX, 160, Unit.PX);
					p.setWidgetTopBottom(cmpImage, 130, Unit.PX, 160, Unit.PX);
				}
				else{
					p.setWidgetBottomHeight(dpBottom, 0, Unit.PX, 90, Unit.PX);
					p.setWidgetTopBottom(cmpImage, 130, Unit.PX, 90, Unit.PX);
				}
				cmpImage.updateUI();
				cmpEdit.updateUI();
				cmpBottomMenu.updateUI();
				cmpTopLeft.updateUI();
				cmpLeftMenu.updateUI();
				cmpTopMenu.updateUI();
				cmpCatalogSearch.updateUI();

			}
			public void onFailure(Throwable caught){
				Window.alert(application.getMessages().editCardFailureDisplay());
			}
		};

		application.getSessionService().getAttributeAsString(CaCaOConstants.SESSIONATTR_EDITOR_STRING, callback);

	}
}
